package demo2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//tcp服务器端
public class TcpEchoService {
    private ServerSocket serverSocket = null;
    public TcpEchoService(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }
    public void start() throws IOException {
        System.out.println("tcp服务器启动");
        ExecutorService threadPool = Executors.newCachedThreadPool();//创建一个可扩容的线程池
        while (true) {
            //1.一个客户端
           /* Socket clientSocket = serverSocket.accept();
            processConnection(clientSocket);*/
            //2.多线程
           /* Socket clientSocket = serverSocket.accept();
            Thread t = new Thread(()->{
                try {
                    processConnection(clientSocket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            t.start();*/
            //3.使用线程池
            Socket clientSocket = serverSocket.accept();
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        processConnection(clientSocket);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });


        }
    }
    //用来处理任务请求
    private void processConnection(Socket clientSocket) throws IOException {
        System.out.printf("客户端上线：[%s:%d]\n",clientSocket.getInetAddress(),clientSocket.getPort());
        //1.获取客户端中的流对象
        try(InputStream inputStream = clientSocket.getInputStream();
                OutputStream outputStream = clientSocket.getOutputStream()) {
            //2.搭配scanner使用
            Scanner scanner = new Scanner(inputStream);
            while (true) {//循环处理客户端的请求
                if(!scanner.hasNext()) {
                    System.out.printf("客户端下线：[%s:%d]\n",clientSocket.getInetAddress(),clientSocket.getPort());
                    break;
                }
                //3.读取请求
                String request = scanner.next();
                //4.处理请求
                String response = process(request);
                //5.写回响应
                outputStream.write(response.getBytes());
                //6.打印日志
                System.out.printf("[%s:%d],request=%s,response=%s\n",clientSocket.getInetAddress(),
                        clientSocket.getPort(),request,response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            clientSocket.close();
        }
    }
    //请求是什么，就返回什么即可
    private String process(String request) {
        return request + '\n';
    }

    public static void main(String[] args) throws IOException {
        TcpEchoService tcpEchoService = new TcpEchoService(1314);
        tcpEchoService.start();
    }

}
